import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:kxy_flutter_app/components/empty_widget.dart';
import 'package:kxy_flutter_app/components/loading_widget.dart';
import 'package:kxy_flutter_app/model/test.dart';
import 'package:kxy_flutter_app/view/examination/test_result_view.dart';
import 'package:kxy_flutter_app/view/examination/widget/test_bottom_bar_widget.dart';
import 'package:provider/provider.dart';
import 'correct_ques_view.dart';
import 'view_model/test_model.dart';
import 'widget/question_header_widget.dart';
import 'widget/question_swiper_widget.dart';

class DoTestView extends StatefulWidget {
  final Test test;

  DoTestView({Key key, this.test}) : super(key: key);

  @override
  _DoTestViewState createState() => _DoTestViewState();
}

class _DoTestViewState extends State<DoTestView> {
  TestModel testModel;
  bool isLoaded = false;
  SwiperController swiperController = new SwiperController();

  @override
  void didChangeDependencies() {
    startExam();
    super.didChangeDependencies();
  }

  Future startExam() async {
    if (!isLoaded) {
      testModel = Provider.of<TestModel>(context);
      isLoaded = true;
      await testModel.startTest(widget.test);
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('开始答题'),
          actions: [correctionWidget(context)],
          elevation: 0,
        ),
        body: Builder(
          builder: (cxt) {
            if (!isLoaded) {
              return LoadingWidget();
            }

            if (testModel.questionList == null || testModel.questionList.length == 0) {
              return EmptyWidget();
            }

            return body();
          },
        ),
      ),
      onWillPop: () async {
        return await showDialog(
          context: context,
          builder: (cxt) {
            return AlertDialog(
              content: Text('您确认要退出答题吗？'),
              actions: [
                FlatButton(
                  child: Text('取消'),
                  onPressed: () => Navigator.pop(context, false),
                ),
                FlatButton(
                  child: Text('确定'),
                  onPressed: () => Navigator.pop(context, true),
                ),
              ],
            );
          },
        );
      },
    );
  }

  Widget body() {
    return Column(
      children: [
        QuestionHeaderWidget(
          question: testModel.current,
          index: testModel.index,
          total: testModel.questionList.length,
        ),
        Expanded(
          child: QuestionSwiperWidget(
            swiperCtrl: swiperController,
            questionList: testModel.questionList,
            onChangeIndex: testModel.changeIndex,
            setAnswer: testModel.setAnswer,
            withResult: true,
            canScroll: false,
          ),
        ),
        TestBottomBarWidget(
          swiperCtrl: swiperController,
          complete: complete,
        ),
      ],
    );
  }

  Widget correctionWidget(BuildContext context) {
    return FlatButton(
      textColor: Theme.of(context).primaryColor,
      child: Text('纠错'),
      onPressed: () {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (cxt) => CorrectQuesView(quesId: testModel.current.quesId),
          ),
        );
      },
    );
  }

  Future complete() async {
    await testModel.complete();
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (cxt) => TestResultView(),
      ),
    );
  }
}
